home *** CD-ROM | disk | FTP | other *** search
/ Aminet 28 / Aminet 28 (1998)(GTI - Schatztruhe)[!][Dec 1998].iso / Aminet / game / board / Crafty-15.19.lha / crafty-15.19 / src / unmake.c < prev    next >
C/C++ Source or Header  |  1998-09-13  |  16KB  |  480 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include "chess.h"
  4. #include "data.h"
  5.  
  6. /* last modified 03/11/98 */
  7. /*
  8. ********************************************************************************
  9. *                                                                              *
  10. *   UnMakeMove() is responsible for updating the position database whenever a  *
  11. *   move is retracted.  it is the exact inverse of MakeMove().                 *
  12. *                                                                              *
  13. ********************************************************************************
  14. */
  15. void UnMakeMove(TREE *tree, int ply, int move, int wtm)
  16. {
  17.   register int piece, from, to, captured, promote;
  18.   BITBOARD bit_move;
  19. /*
  20.  ----------------------------------------------------------
  21. |                                                          |
  22. |   first, take care of the hash key if there's a possible |
  23. |   enpassant pawn capture.                                |
  24. |                                                          |
  25.  ----------------------------------------------------------
  26. */
  27.   HashKey=tree->save_hash_key[ply];
  28.   PawnHashKey=tree->save_pawn_hash_key[ply];
  29. /*
  30.  ----------------------------------------------------------
  31. |                                                          |
  32. |   now do the piece-specific things by calling the        |
  33. |   appropriate routine.                                   |
  34. |                                                          |
  35.  ----------------------------------------------------------
  36. */
  37.   piece=Piece(move);
  38.   from=From(move);
  39.   to=To(move);
  40.   captured=Captured(move);
  41.   promote=Promote(move);
  42. UnMakePieceMove:
  43.   SetRL90(from,OccupiedRL90);
  44.   SetRL45(from,OccupiedRL45);
  45.   SetRR45(from,OccupiedRR45);
  46.   ClearRL90(to,OccupiedRL90);
  47.   ClearRL45(to,OccupiedRL45);
  48.   ClearRR45(to,OccupiedRR45);
  49.   bit_move=Or(set_mask[from],set_mask[to]);
  50.   PieceOnSquare(to)=0;
  51.   switch (piece) {
  52.  
  53. /*
  54. ********************************************************************************
  55. *                                                                              *
  56. *   unmake pawn moves.                                                         *
  57. *                                                                              *
  58. ********************************************************************************
  59. */
  60.   case pawn:
  61.     if (wtm) {
  62.       ClearSet(bit_move,WhitePawns);
  63.       ClearSet(bit_move,WhitePieces);
  64.       PieceOnSquare(from)=pawn;
  65.       if (captured == 1) {
  66.         if(EnPassant(ply) == to) {
  67.           TotalPieces++;
  68.           SetRL90(to-8,OccupiedRL90);
  69.           SetRL45(to-8,OccupiedRL45);
  70.           SetRR45(to-8,OccupiedRR45);
  71.           Set(to-8,BlackPawns);
  72.           Set(to-8,BlackPieces);
  73.           PieceOnSquare(to-8)=-pawn;
  74.           Material-=PAWN_VALUE;
  75.           TotalBlackPawns++;
  76.           captured=0;
  77.         }
  78.       }
  79. /*
  80.  --------------------------------------------------------------------
  81. |                                                                    |
  82. |  if this is a pawn promotion, remove the pawn from the counts      |
  83. |  then update the correct piece board to reflect the piece just     |
  84. |  created.                                                          |
  85. |                                                                    |
  86.  --------------------------------------------------------------------
  87. */
  88.       if (promote) {
  89.         TotalWhitePawns++;
  90.         Material+=PAWN_VALUE;
  91.         Clear(to,WhitePawns);
  92.         Clear(to,WhitePieces);
  93.         switch (promote) {
  94.         case knight:
  95.           Clear(to,WhiteKnights);
  96.           TotalWhitePieces-=knight_v;
  97.           WhiteMinors--;
  98.           Material-=KNIGHT_VALUE;
  99.           break;
  100.         case bishop:
  101.           Clear(to,WhiteBishops);
  102.           Clear(to,BishopsQueens);
  103.           TotalWhitePieces-=bishop_v;
  104.           WhiteMinors--;
  105.           Material-=BISHOP_VALUE;
  106.           break;
  107.         case rook:
  108.           Clear(to,WhiteRooks);
  109.           Clear(to,RooksQueens);
  110.           TotalWhitePieces-=rook_v;
  111.           WhiteMajors--;
  112.           Material-=ROOK_VALUE;
  113.           break;
  114.         case queen:
  115.           Clear(to,WhiteQueens);
  116.           Clear(to,BishopsQueens);
  117.           Clear(to,RooksQueens);
  118.           TotalWhitePieces-=queen_v;
  119.           WhiteMajors-=2;
  120.           Material-=QUEEN_VALUE;
  121.           break;
  122.         }
  123.       }
  124.     }
  125.     else {
  126.       ClearSet(bit_move,BlackPawns);
  127.       ClearSet(bit_move,BlackPieces);
  128.       PieceOnSquare(from)=-pawn;
  129.       if (captured == 1) {
  130.         if(EnPassant(ply) == to) {
  131.           TotalPieces++;
  132.           SetRL90(to+8,OccupiedRL90);
  133.           SetRL45(to+8,OccupiedRL45);
  134.           SetRR45(to+8,OccupiedRR45);
  135.           Set(to+8,WhitePawns);
  136.           Set(to+8,WhitePieces);
  137.           PieceOnSquare(to+8)=pawn;
  138.           Material+=PAWN_VALUE;
  139.           TotalWhitePawns++;
  140.           captured=0;
  141.         }
  142.       }
  143. /*
  144.  --------------------------------------------------------------------
  145. |                                                                    |
  146. |  if this is a pawn promotion, remove the pawn from the counts      |
  147. |  then update the correct piece board to reflect the piece just     |
  148. |  created.                                                          |
  149. |                                                                    |
  150.  --------------------------------------------------------------------
  151. */
  152.       if (promote) {
  153.         TotalBlackPawns++;
  154.         Material-=PAWN_VALUE;
  155.         Clear(to,BlackPawns);
  156.         Clear(to,BlackPieces);
  157.         switch (promote) {
  158.         case knight:
  159.           Clear(to,BlackKnights);
  160.           TotalBlackPieces-=knight_v;
  161.           BlackMinors--;
  162.           Material+=KNIGHT_VALUE;
  163.           break;
  164.         case bishop:
  165.           Clear(to,BlackBishops);
  166.           Clear(to,BishopsQueens);
  167.           TotalBlackPieces-=bishop_v;
  168.           BlackMinors--;
  169.           Material+=BISHOP_VALUE;
  170.           break;
  171.         case rook:
  172.           Clear(to,BlackRooks);
  173.           Clear(to,RooksQueens);
  174.           TotalBlackPieces-=rook_v;
  175.           BlackMajors--;
  176.           Material+=ROOK_VALUE;
  177.           break;
  178.         case queen:
  179.           Clear(to,BlackQueens);
  180.           Clear(to,BishopsQueens);
  181.           Clear(to,RooksQueens);
  182.           TotalBlackPieces-=queen_v;
  183.           BlackMajors-=2;
  184.           Material+=QUEEN_VALUE;
  185.           break;
  186.         }
  187.       }
  188.     }
  189.     break;
  190.  
  191. /*
  192. ********************************************************************************
  193. *                                                                              *
  194. *   unmake knight moves.                                                       *
  195. *                                                                              *
  196. ********************************************************************************
  197. */
  198.   case knight:
  199.     if (wtm) {
  200.       ClearSet(bit_move,WhiteKnights);
  201.       ClearSet(bit_move,WhitePieces);
  202.       PieceOnSquare(from)=knight;
  203.     }
  204.     else {
  205.       ClearSet(bit_move,BlackKnights);
  206.       ClearSet(bit_move,BlackPieces);
  207.       PieceOnSquare(from)=-knight;
  208.     }
  209.     break;
  210.  
  211. /*
  212. ********************************************************************************
  213. *                                                                              *
  214. *   unmake bishop moves.                                                       *
  215. *                                                                              *
  216. ********************************************************************************
  217. */
  218.   case bishop:
  219.     ClearSet(bit_move,BishopsQueens);
  220.     if (wtm) {
  221.       ClearSet(bit_move,WhiteBishops);
  222.       ClearSet(bit_move,WhitePieces);
  223.       PieceOnSquare(from)=bishop;
  224.     }
  225.     else {
  226.       ClearSet(bit_move,BlackBishops);
  227.       ClearSet(bit_move,BlackPieces);
  228.       PieceOnSquare(from)=-bishop;
  229.     }
  230.     break;
  231. /*
  232. ********************************************************************************
  233. *                                                                              *
  234. *   unmake rook moves.                                                         *
  235. *                                                                              *
  236. **********************